import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'

Vue.use(Vuex);
Vue.prototype.$ajax = axios

export const store = new Vuex.Store({
    //设置状态
    state:{
        newsList:[],
        goosList:[],
        newsCate:[],
        newsListByCate:[],
        newDetails:[],
        commenList:[],
        productCate:[],
        productListByCate:[],
        cart:[],
        ids:[],
        cartsNum:0,
        totalMoney:0,
    },
    //获取状态
    getters:{
        newsList:state=>{
            return state.newsList
        },
        goosList:state=>{
            return state.goosList
        },
        newsCate:state=>{
            return state.newsCate
        },
        newsListByCate:state=>{
            return state.newsListByCate
        },
        newDetails:state=>{
            return state.newDetails
        },
        commenList:state=>{
            return state.commenList
        },
        productCate:state=>{
            return state.productCate
        },
        productListByCate:state=>{
            return state.productListByCate
        },
        cart:state=>{
            return state.cart
        },
        totalMoney:state=>{
            return state.totalMoney
        },
        cartsNum:state=>{
            return state.cartsNum
        }
    },
    //操作
    mutations:{
        getNewsList:(state,payload)=>{
            axios.post("http://localhost:3000/news/list",payload)
            .then((res)=>{
                state.newsList=res.data.rows;
            })
        },
        getGoosList:(state,payload)=>{
            axios.post("http://localhost:3000/goods/list",payload)
            .then((res)=>{
                state.goosList=res.data.rows;
            })
        },
        getNewsCate:(state,payload)=>{
            axios.get("http://localhost:3000/classify/list/1",payload)
            .then((res)=>{
                state.newsCate=res.data;
            })
        },
        getNewsListByCate:(state,payload)=>{
            axios.post("http://localhost:3000/news/list",payload)
            .then((res)=>{
                state.newsListByCate=res.data.rows;
            })
        },
        getNewsDetails:(state,payload)=>{
            axios.get("http://localhost:3000/news/data/"+payload)
            .then((res)=>{
                state.newDetails=res.data;
            })
        },
        getCommenList:(state,payload)=>{
            axios.post("http://localhost:3000/comment/list",payload)
            .then((res)=>{
                state.commenList=res.data.rows;
            })
        },
        getProductCate:(state,payload)=>{
            axios.get("http://localhost:3000/classify/list/3",payload)
            .then((res)=>{
                state.productCate=res.data;
            })
        },
        getProductListByCate:(state,payload)=>{
            axios.post("http://localhost:3000/goods/list",payload)
            .then((res)=>{
                state.productListByCate=res.data.rows;
            })
        },
        getAddCart:(state,payload)=>{
            if(state.ids.indexOf(payload.id)==-1){
                state.ids.push(payload.id);
                axios.get(`http://localhost:3000/goods/data/`+payload.id)
                .then(res=>{   
                    res.data.num=1;
                    state.cart.unshift(res.data)  
                    state.cartsNum=0;
                    state.totalMoney=0;
                    for(var j=0;j<state.cart.length;j++){
                        state.cartsNum+=Number(state.cart[j].num)
                        state.totalMoney+=Number(state.cart[j].num)*Number(state.cart[j].price)
                    }
                })
            }else{
                for (var i = 0; i < state.cart.length ;i++) {
                    if (state.cart[i]._id == payload.id) {
                        break;
                    }
                }
                state.cart[i].num+=1    
                state.cartNum=0;
                state.totalMoney=0;
                for(var j=0;j<state.cart.length;j++){
                    state.cartNum+=Number(state.cart[j].num)
                    state.totalMoney+=Number(state.cart[j].num)*Number(state.cart[j].price)
                }                
            }
        }
    },
    //异步
    actions:{
        getNewsList:({commit},payload)=>{
            commit('getNewsList',payload);
        },
        getGoosList:({commit},payload)=>{
            commit('getGoosList',payload);
        },
        getNewsCate:({commit},payload)=>{
            commit('getNewsCate',payload);
        },
        getNewsListByCate:({commit},payload)=>{
            commit('getNewsListByCate',payload);
        },
        getNewsDetails:({commit},payload)=>{
            commit('getNewsDetails',payload);
        },
        getCommenList:({commit},payload)=>{
            commit('getCommenList',payload);
        },
        getProductCate:({commit},payload)=>{
            commit('getProductCate',payload);
        },
        getProductListByCate:({commit},payload)=>{
            commit('getProductListByCate',payload);
        },
        getAddCart:({commit},payload)=>{
            commit('getAddCart',payload);
        }
    }

});

